<?php

namespace App\Models\Act;

use Illuminate\Database\Eloquent\Model;
use Laracasts\Presenter\PresentableTrait;

class Question extends Model
{
    use PresentableTrait;

	protected $table = 'act_questions';

    protected $guarded = array();

	protected $presenter = 'App\Http\Presenters\QuestionPresenter';

    //protected $with = ['corrects'];
    /**
     * 题目选项
     *
     * @return void
     */
    public function options()
    {
        return $this->hasMany('\App\Models\Act\Option');
    }

    /**
     * 正确答案
     *
     * @return void
     */
    public function corrects()
    {
        return $this->options()->where('isCorrect', STATUS_NORMAL);
    }

    public function correct()
    {
        if($this->type == QUESTION_BLANK)
            return $this->options->first()->content;

        if($this->type == QUESTION_SELECT){
            $result = $this->corrects()->get('id')->toArray();
            $result = array_values(collect($result)->flatten()->toArray());
            return implode(',',$result);
        }
    }

}